package com.wave.traffic.util;


import com.wave.traffic.enums.NetControllerEnum;
import com.wave.traffic.task.HKCameraTask;
import com.wave.traffic.task.ServerInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.net.Socket;
import java.util.Date;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;

/***
 * @Description
 * @Author chenjian
 * @Date 2022/8/1 18:55
 */
@Component
public class TimerUtil {

    private final static Logger logger = LoggerFactory.getLogger(TimerUtil.class);

    private static Timer timer;

    /***
     * @description 取消原本的计时器，同时新建一个定时器
     * @param delay 毫秒数
     * @date 2022/8/3 11:11
     * @author chenjian
     */
    public static void call(Socket netController1,Socket netController2,Socket netController3, Long delay) {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                try {
                    logger.info("延时任务：控制器213发送人行灯红灯指令,车辆灯绿灯指令");
                    //SocketClientUtil.sendData(netController1, NetControllerEnum.WRITE_DO1_OPEN_COMMAND.getCode());
                    Map<Socket, String> map = new ConcurrentHashMap<>();
                    map.put(netController1, NetControllerEnum.WRITE_DO1_OPEN_COMMAND.getCode());
                    HKCameraTask.blockingQueue1.put(map);
                    logger.info("延时任务：控制器213发送人行灯红灯指令,车辆灯绿灯指令-1");

                    //SocketClientUtil.sendData(netController1, NetControllerEnum.WRITE_DO2_CLOSE_COMMAND.getCode());
                    map = new ConcurrentHashMap<>();
                    map.put(netController1, NetControllerEnum.WRITE_DO2_CLOSE_COMMAND.getCode());
                    HKCameraTask.blockingQueue1.put(map);
                    logger.info("延时任务：控制器213发送人行灯红灯指令,车辆灯绿灯指令-2");

                    //SocketClientUtil.sendData(netController2, NetControllerEnum.WRITE_DO1_CLOSE_COMMAND.getCode());
                    map = new ConcurrentHashMap<>();
                    map.put(netController2, NetControllerEnum.WRITE_DO1_CLOSE_COMMAND.getCode());
                    HKCameraTask.blockingQueue2.put(map);
                    logger.info("延时任务：控制器212发送人行灯红灯指令");

                    //SocketClientUtil.sendData(netController3, NetControllerEnum.WRITE_DO1_CLOSE_COMMAND.getCode());
                    map = new ConcurrentHashMap<>();
                    map.put(netController3, NetControllerEnum.WRITE_DO1_CLOSE_COMMAND.getCode());
                    HKCameraTask.blockingQueue3.put(map);
                    logger.info("延时任务：控制器215发送人行灯红灯指令");
                } catch (Exception e) {
                    logger.error("延时任务执行异常,e:", e);
                    throw new RuntimeException(e);
                }
            }
        }, delay);
    }
}